//
//  CFileUpload.m
//  OC_Test_1
//
//  Created by 四小鬼 on 16/9/12.
//  Copyright © 2016年 四小鬼. All rights reserved.
//

#import "CFileUpload.h"

@implementation CFileModel

+ (CFileModel *) initWithFile:(id)file name:(NSString *)name{
    CFileModel *model = [CFileModel new];
    model.file = file;
    model.name = name;
    return model;
}

@end

@interface CFileUpload ()
@property (nonatomic, strong) NSMutableArray *fileModels;
@property (nonatomic, strong) NSMutableArray *keyArray;
@property (nonatomic, strong) NSMutableArray *requestArray;
@end

@implementation CFileUpload

- (NSMutableArray *)keyArray{
    if (_keyArray == nil) {
        _keyArray = [NSMutableArray array];
    }
    return _keyArray;
}

- (NSMutableArray *)requestArray{
    if (_requestArray == nil) {
        _requestArray = [NSMutableArray array];
    }
    return _requestArray;
}

- (CFileModel *) getFileModelWith:(id) file{
    NSInteger index = [self.keyArray indexOfObject:file];
    return [_fileModels objectAtIndex:index];
}

- (void) uploadFiles:(id) file progress:(void (^)(CFileUpload *upload, float per))progress finish:(void(^)(CFileUpload *upload, NSArray *models, NSError *error)) finish{
    NSMutableArray *files = [NSMutableArray array];
    if ([file isKindOfClass:[NSArray class]]) {
        [files addObjectsFromArray:file];
    }else if([file isKindOfClass:[CFileModel class]]){
        [files addObject:file];
    }else{
        return;
    }
    _fileModels = files;
    
    __block NSInteger count = files.count;
    for (CFileModel *model in files) {
        if (![model isKindOfClass:[CFileModel class]]) {
            return;
        }
        [self.keyArray addObject:model.file];
        
        void (^fu_finish)(CFileUpload *upload, NSArray *model, NSError *error) = ^(CFileUpload *upload, NSArray *models, NSError *error){
            count --;
            if (files.count > 1 && progress) {
                progress(self, 1 - count * 1.0 / files.count);
            }
            if (count == 0 && finish) {
                if (files.count > 1) {
                    finish(self, _fileModels, error);
                }else{
                    finish(self, models, error);
                }
            }
        };
        void (^fu_progress)(CFileUpload *upload, float per) = ^(CFileUpload *upload, float per){
            if (files.count > 1) {
                return ;
            }else if(progress){
                progress(upload, per);
            }
        };
        
        if ([model.file isKindOfClass:[NSString class]]) {
            [self.requestArray addObject:[self uploadFileByPath:model.file progress:fu_progress finish:fu_finish]];
        }else if([model.file isKindOfClass:[NSData class]]){
            [self.requestArray addObject:[self uploadFileByData:model.file progress:fu_progress finish:fu_finish]];
        }
    }
}

- (void) stopUpload{
    for (id request in self.requestArray) {
        [request cancel];
    }
}

- (void) stopUpload:(id) file{
    NSInteger index = [self.keyArray indexOfObject:file];
    [[self.requestArray objectAtIndex:index] cancel];
}
@end

@implementation CFileUpload(CFileUploadPrivate)

- (id) uploadFileByPath:(NSString *)path progress:(void (^)(CFileUpload *upload, float per))progress finish:(void(^)(CFileUpload *upload, NSArray *models, NSError *error)) finish{
    return self;
}

- (id) uploadFileByData:(NSData *)data progress:(void (^)(CFileUpload *upload, float per))progress finish:(void(^)(CFileUpload *upload, NSArray *models, NSError *error)) finish{
    return self;
}

@end
